package star.基础.滑动窗口最大值;

import star.Utils;

import java.util.LinkedList;

public class MaxSlidingWindow {
    public static void main(String[] args) {
        int[] nums = {9,10,9,-7,-4,-8,2,-6};
        int k = 5;
        int[] result = new MaxSlidingWindow().maxSlidingWindow(nums, k);
        Utils.traversal(result);
    }
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums == null || nums.length == 0 || k == 0) return new int[0];
        LinkedList<Integer> list = new LinkedList<>();
        int[] res = new int[nums.length - k + 1];
        int big = Integer.MIN_VALUE;

        for (int i = 0;i < k;i++){
            big = big > nums[i] ? big : nums[i];
        }
        res[0] = big;
        for (int i = 0; i < nums.length-k;i++){
            big = Integer.MIN_VALUE;
            if (res[i] > nums[i+k] && nums[i]!=res[i]){
                res[i+1] = res[i];
            }else {
                for (int j = i+1;j <= i+k;j++){
                    big = big > nums[j] ? big : nums[j];
                }
                res[i+1] = big;
            }
        }
        return res;
    }
}
